# -*- coding: iso-8859-15 -*-

#
# Copyright 2007 Luciano Xumerle. All rights reserved.
# Luciano Xumerle <luciano.xumerle@gmail.com>
#
# This file is part of cnxrename.
#
# Cnxrename is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Cnxrename is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cnxrename.  If not, see <http://www.gnu.org/licenses/>.
#

import re
import string
import os
import urllib

class XFile:

	src=""
	dest=""
	path=""
	file=""
	extension=""
	srcext=""
	maxlength=0
	isDir=False
	isSel=True

	def __init__( self, namefile, isDirectory, maxDestLength):
		self.maxlength=maxDestLength
		pos = namefile.rfind( os.sep )
		self.file = namefile
		(self.path, self.src) = os.path.split(namefile)
		if(self.path == ""):
			self.path = "."
		self.srcext = self.getFileExtension( self.src )
		self.extension = self.getFileExtension( self.src ).lower()
		self.src = self.delFileExtension( self.src )
		self.dest = self.src
		self.isDir = isDirectory
		self.isSel = True
		self.getShortenDestination()

	def getDestFile(self):
		self.getShortenDestination()
		return self.getPath() + os.sep + self.getDest() + self.getExtension()

	def getSrc(self):
		return self.src

	def getSrcFile(self):
		return self.getSrc() + self.getExtension()

	def getDest(self):
		self.getShortenDestination()
		if self.dest == "":
			return self.src
		return self.dest

	def setDest( self, destination ):
		self.dest = destination
		self.getShortenDestination()

	def getPath(self):
		return self.path

	def getFile(self):
		return self.file

	def getExtension(self):
		if len(self.extension) == 0:
			return ""
		return self.extension.lower()

	def setExtension( self, theextension ):
		self.extension = theextension
		if len(self.extension)>0 and self.extension[ 0 ] != '.':
			self.extension = '.' + self.extension

	def getIsSel(self):
		return self.isSel

	def setIsSel( self, isSelected ):
		self.isSel = self.isSelected

	def toBeRenamed(self):
		return self.getDestFile() != self.getFile()

	def toString(self):
		return self.getPath() + os.sep + " " + self.getSrc() + self.srcext + " --> " + self.getDest() + self.getExtension()

	def isExtension( self, str ):
		ln = len(str)
		return ln > 1 and ln < 6 and str[ 0 ] == '.' and str[1:].isalnum()

	def getFileExtension( self, file ):
		(shortname, extension) = os.path.splitext(file)
		if not self.isExtension( extension ):
			return ""
		(shortname2, extension2) = os.path.splitext(shortname)
		if extension2 == ".tar":
			return extension2 + extension
		return extension

	def delFileExtension( self, file ):
		return file[ 0:len(file) - len(self.getFileExtension( file )) ]

	def renameFile( self ):
		self.getShortenDestination()
		source=self.path + os.sep + self.src + self.srcext
		destination=self.path + os.sep + self.dest + self.extension
		if not os.path.isfile( destination ):
			try:
				os.rename ( source, destination )
				return True
			except Exception, e:
				return False
		return False

	def getShortenDestination( self ):
		destlength=self.maxlength-len(self.extension)
		if self.maxlength>0 and len(self.dest)>destlength:
			i=destlength-1
			while i>0 and self.dest[i].isalpha():
				i=i-1
			while i>0 and not self.dest[i].isalpha():
				i=i-1
			if i==0:
				i=destlength
			self.dest=self.dest[:i+1]

